#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include "SystemCatalog.h"

namespace MiniDB {
// 表定义
struct TableInfo {
    std::string name;
    std::vector<ColumnInfo> columns;
    std::unordered_map<std::string, size_t> column_index; // column_name -> index

    TableInfo() : name(""), record_count(0), total_size(0) {}
    TableInfo(const std::string& table_name) : name(table_name), record_count(0), total_size(0) {}

    void addColumn(const ColumnInfo& col) {
        column_index[col.name] = columns.size();
        columns.push_back(col);
    }

    bool hasColumn(const std::string& col_name) const {
        return column_index.find(col_name) != column_index.end();
    }

    const ColumnInfo* getColumn(const std::string& col_name) const {
        auto it = column_index.find(col_name);
        if (it != column_index.end()) {
            return &columns[it->second];
        }
        return nullptr;
    }

    // For compatibility
    size_t record_count;
    size_t total_size;
};

// 符号表
class SymbolTable {
public:
    SymbolTable() = default;

    // 表管理
    bool addTable(const TableInfo& table);
    bool hasTable(const std::string& table_name) const;
    const TableInfo* getTable(const std::string& table_name) const;
    std::vector<std::string> getTableNames() const;

    // 列管理
    bool hasColumn(const std::string& table_name, const std::string& column_name) const;
    const ColumnInfo* getColumn(const std::string& table_name, const std::string& column_name) const;

    // 类型检查
    bool areTypesCompatible(DataType type1, DataType type2) const;
    DataType getResultType(DataType type1, DataType type2) const;

    void clear();

private:
    std::unordered_map<std::string, TableInfo> tables_;
};

} // namespace MiniDB
